<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


// 应用公共文件
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


/**
 * send_email
 *
 * 发送邮件函数
 *
 * @param string $send_address 发地址
 * @param string $title 标题
 * @param string $content 内容
 * @return bool result 发送结果
 */
function send_email($send_address, $title, $content)
{
    $mail = new PHPMailer(true);

    try {
        //Server settings
//        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->SMTPDebug = SMTP::DEBUG_OFF;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->CharSet = 'utf-8';
        $mail->Host = 'smtp.163.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth = true;                                   // Enable SMTP authentication
        $mail->Username = 'ygf751@163.com';                     // SMTP username
        $mail->Password = 'y135159+';                               // SMTP password
        // $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;         // ssl协议
        $mail->Port = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom('ygf751@163.com', '听雨');     // 发件人
        $mail->addAddress($send_address);     // 收件人

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $title;     // 主题
        $mail->Body = $content;     // 内容

        return $mail->send();
    } catch (Exception $e) {
        echo '邮件发送失败: ', $mail->ErrorInfo;
        exception($mail->ErrorInfo, 1001);
    }
}


/**
 * replace_str
 *
 * 替换字符串， span -> a
 *
 * @param string $data 待操作字符串
 * @return string newString 替换后的字符串
 */
function replace_str($data)
{
    return str_replace('span', 'a', $data);
}


/**
 * str_to_array
 *
 * 字符串分割为数组
 *
 * @param string $data 待操作字符串
 * @return array newarray 分割后得到的数组
 */
function str_to_array($data)
{
    return explode('|', $data);
}